/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.web.core.controller;

import com.jiantong.user.dto.OrganizationSuggestion;
import com.jiantong.user.enums.OrganizationType;
import com.jiantong.user.service.OrganizationReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.exception.JsonResponseException;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : panxin
 */
@Slf4j
@RestController
@RequestMapping
public class CommonOrganizations {

    @RpcConsumer
    private OrganizationReadService organizationReadService;

    @RequestMapping(value = "/api/common/suggestion-government", method = RequestMethod.GET)
    public List<OrganizationSuggestion> governmentSuggestion(@RequestParam String name) {
        Response<List<OrganizationSuggestion>> resp =
                organizationReadService.suggestByNameAndType(name, OrganizationType.GOVERNMENT);
        if (resp.isSuccess()) {
            log.error("failed to suggest government by name = {}, cause : {}", name, resp.getError());
            throw new JsonResponseException(resp.getError());
        }
        return resp.getResult();
    }

    @RequestMapping(value = "/api/common/suggestion-butcher-factory", method = RequestMethod.GET)
    public List<OrganizationSuggestion> butcherFactorySuggestion(@RequestParam String name) {
        Response<List<OrganizationSuggestion>> resp =
                organizationReadService.suggestByNameAndType(name, OrganizationType.BUTCHER);
        if (resp.isSuccess()) {
            log.error("failed to suggest butcher factory by name = {}, cause : {}", name, resp.getError());
            throw new JsonResponseException(resp.getError());
        }
        return resp.getResult();
    }

    @RequestMapping(value = "/api/common/suggestion-breed-factory", method = RequestMethod.GET)
    public List<OrganizationSuggestion> breedFactorySuggestion(@RequestParam String name) {
        Response<List<OrganizationSuggestion>> resp =
                organizationReadService.suggestByNameAndType(name, OrganizationType.BREEDER);
        if (resp.isSuccess()) {
            log.error("failed to suggest breed factory by name = {}, cause : {}", name, resp.getError());
            throw new JsonResponseException(resp.getError());
        }
        return resp.getResult();
    }

}
